package com.my.app.tools.ui;

import com.google.common.collect.Lists;
import com.my.app.tools.core.business.CoreBusiness;
import com.my.app.tools.pojo.vo.EnterpriseInfoVO;
import com.my.app.tools.pojo.vo.PeopleInfoVO;
import com.my.app.tools.util.AppThreadUtil;
import com.my.app.tools.util.LogUtil;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Vector;

/**
 * ui操作
 *
 * @author guochaohui
 * @return
 * @date 2020-03-16 10:17
 */
public class AppUiSpiderEnterpriseInfo {

    private static JPanel mainPanel;

    private static JPanel controlPanel;
    private static JLabel keywordLabel;
    private static JTextField keywordTextField;
    private static JLabel maxCountLabel;
    private static JComboBox maxCountComboBox;
    private static JButton spiderButton;

    private static JPanel resultPanel;
    private static JScrollPane resultScrollPane;
    private static JTable resultTable;

    /**
     * 初始化UI
     *
     * @return
     * @author guochaohui
     * @date 2020-03-16 10:20
     */
    public static final JPanel InitUi() {

        // 所有控件面板
        mainPanel = new JPanel(null);
        mainPanel.setBounds(0, 0, AppUiMain.appWindowWidth, AppUiMain.appWindowHeight);

        // 所有控件
        controlPanel = new JPanel(null);
        controlPanel.setBounds(10, 10, 610, 60);
        controlPanel.setBorder(BorderFactory.createTitledBorder("控制按钮"));

        keywordLabel = new JLabel("企业名称关键字");
        keywordLabel.setBounds(30, 20, 100, 20);
        controlPanel.add(keywordLabel);

        keywordTextField = new JTextField();
        keywordTextField.setBounds(130, 22, 100, 20);
        controlPanel.add(keywordTextField);

        maxCountLabel = new JLabel("最大抓取数量");
        maxCountLabel.setBounds(260, 20, 100, 20);
        controlPanel.add(maxCountLabel);

        maxCountComboBox = new JComboBox(new Object[]{20, 60, 100});
        maxCountComboBox.setBounds(350, 22, 60, 20);
        controlPanel.add(maxCountComboBox);

        spiderButton = new JButton("抓取数据");
        spiderButton.setBounds(480, 22, 100, 20);
        spiderButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                spiderData();
            }
        });
        controlPanel.add(spiderButton);
        // 数据结果
        resultPanel = new JPanel(null);
        resultPanel.setBounds(10, 70, 610, 370);
        resultPanel.setBorder(BorderFactory.createTitledBorder("数据结果-点击左键选择复制"));

        // 解析结果列表
        resultTable = new JTable(new Vector(), new Vector(Lists.newArrayList("序号", "信用主体名称", "统一社会信用代码", "工商注册号/登记证号")));
        resultTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        resultTable.setBounds(10, 22, 590, 340);
        resultTable.getTableHeader().setResizingAllowed(false);
        resultTable.getTableHeader().setReorderingAllowed(false);
        resultTable.setRowHeight(20);
        resultTable.setPreferredScrollableViewportSize(new Dimension(590, 340));
        resultTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        resultTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        AppUiMain.setTableColumnWidth(resultTable, Lists.newArrayList(40, 230, 160, 140));
        resultTable.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                AppUiMain.clickTableShowCopyMenu(e, resultTable);
            }
        });
        resultScrollPane = new JScrollPane(resultTable);
        resultScrollPane.setBounds(10, 22, 590, 340);
        resultPanel.add(resultScrollPane);

        mainPanel.add(controlPanel);
        mainPanel.add(resultPanel);
        return mainPanel;
    }

    /**
     * 生成数据
     *
     * @return
     * @author guochaohui
     * @date 2020-04-01 16:53
     */
    private static void spiderData() {
        if (StringUtils.isBlank(keywordTextField.getText())) {
            AppUiMain.errorMsgBox("请填写“企业名称关键字”");
            return;
        }
        AppUiMain.showLoading("数据抓取中，请稍候......");
        AppThreadUtil.INSTANCE.getExecutor().submit(() -> {
            try {
                String keyword = keywordTextField.getText();
                Object maxCountObject = maxCountComboBox.getModel().getSelectedItem();
                Integer spiderCount = Integer.valueOf(String.valueOf(maxCountObject));
                List<EnterpriseInfoVO> list = CoreBusiness.spiderEnterpriseInfo(keyword, spiderCount);
                Vector<Vector> datas = new Vector<>();
                for (int i = 0; i < list.size(); i++) {
                    Vector vector = new Vector(Lists.newArrayList(
                            // 序号
                            i + 1
                            // 企业名称
                            , list.get(i).getEnterpriseName()
                            // 社会信用代码
                            , list.get(i).getEnterpriseCreditCode()
                            // 注册登记号
                            , list.get(i).getEnterpriseRegisterNo()
                    ));
                    datas.add(vector);
                }
                AppUiMain.setTableData(resultTable, datas, false);
            } catch (Exception e) {
                e.printStackTrace();
                LogUtil.error(e.getMessage());
                AppUiMain.errorMsgBox(e.getMessage());
            } finally {
                AppUiMain.hideLoading();
            }
        });
    }

}
